﻿/*
(c) 2010 - Open Source Business Foundation e. V. (www.osbf.de)
This software is provided under Microsoft Reciprocal License (Ms-RL)
(http://www.microsoft.com/opensource/licenses.mspx#Ms-RL)
All other rights reserved
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MicroFormatProcessing
{
    public class MicroFormatUtil
    {
        /// <summary>
        /// Compilation of convenience methods for Microformats 
        /// </summary>
        public static void StoreMicroFormats(string mfUser, string mfData)
        {
            DateTime mfReceived = DateTime.UtcNow;
            object jsonObject = JSON.JsonDecode(mfData);
            if (jsonObject != null)
            {
                Hashtable metaData = (Hashtable)jsonObject;
                string mfUrl = (string)metaData["mfUrl"];
                ArrayList mfTags = (ArrayList)metaData["mfTags"];
                ArrayList mfSelected = (ArrayList)metaData["mfSelected"];
                if (mfTags == null)
                {
                    mfTags = new ArrayList();
                }
                mfTags.Remove("Me");
                mfTags.Remove("Everything");
                if (mfTags.Count == 0)
                {
                    mfTags.Add("Untagged");
                }
                for (int i = 0; i < mfSelected.Count; i++)
                {
                    StoreMicroFormat(mfUser, mfUrl, mfReceived, i + 1, (Hashtable)mfSelected[i], mfTags);
                }
            }
        }

        private static void StoreMicroFormat(string userId, string mfUrl, DateTime mfReceived, int mfIndex, Hashtable mfContent, ArrayList mfTags)
        {
            MicroFormat microFormat = new MicroFormat();
            microFormat.SetUserId(userId);
            microFormat.Name = (string)mfContent["mfName"];
            microFormat.FoundOnDate = mfReceived;
            microFormat.FoundOnUrl = mfUrl;
            microFormat.Type = (string)mfContent["mfType"];
            foreach (string propertyName in mfContent.Keys)
            {
                if (!"mfType".Equals(propertyName) && !"mfName".Equals(propertyName))
                {
                    microFormat.SetProperty(propertyName, (string)mfContent[propertyName]);
                }
            }
            foreach (string tagName in mfTags)
            {
                microFormat.AddTag(tagName);
            }
            microFormat.SerializeAttributes();

            MicroFormatRepository mfRepository = new MicroFormatRepository();
            MicroFormat oldMicroFormat = findOldMicroFormat(microFormat);
            if (oldMicroFormat != null)
            {
                foreach (string oldTag in oldMicroFormat.GetTagArray())
                {
                    if (!"Untagged".Equals(oldTag))
                    {
                        microFormat.AddTag(oldTag);
                    }
                }
                mfRepository.Update(microFormat, true);
            }
            else
            {
                mfRepository.Add(microFormat);
            }

            TagRepository tagRepository = new TagRepository();
            foreach (string tagName in microFormat.GetTagArray())
            {
                bool tagFound = false;
                foreach (Tag tag in tagRepository.FindByUserIdAndTagName(microFormat.GetUserId(), tagName))
                {
                    tagFound = true;
                    if (!tag.HasReference(microFormat.RowKey))
                    {
                        tag.AddReference(microFormat.RowKey);
                        tagRepository.Update(tag, false);
                    }
                }
                if (!tagFound)
                {
                    Tag tag = new Tag(microFormat.GetUserId(), tagName);
                    tag.AddReference(microFormat.RowKey);
                    tagRepository.Add(tag);
                }
            }
        }

        private static MicroFormat findOldMicroFormat(MicroFormat newMicroFormat)
        {
            // TODO: handle microFormats with same hash but different properties
            MicroFormat result = null;
            MicroFormatRepository mfRepository = new MicroFormatRepository();
            foreach (MicroFormat microFormat in mfRepository.FindByUserIdAndMfId(newMicroFormat.GetUserId(), newMicroFormat.RowKey))
            {
                if (microFormat.PropertyNames.Equals(newMicroFormat.PropertyNames) && microFormat.PropertyValues.Equals(newMicroFormat.PropertyValues))
                {
                    result = microFormat;
                }
            }
            return result;
        }
    }
}
